package com.kl.websocket.fileAppenderLog;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.kl.websocket.LoggerDisruptorQueue;
import com.kl.websocket.LoggerMessage;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.DateFormat;

/**
 * 监听logback消息，然后推送到前端
 */
@Service
public class ProcessLogNoFileFilter extends Filter<ILoggingEvent> {

    @Override
    public FilterReply decide(ILoggingEvent event) {
        LoggerMessage loggerMessage = new LoggerMessage(
                event.getMessage()
                , DateFormat.getDateTimeInstance().format(new Date(event.getTimeStamp())),
                event.getThreadName(),
                event.getLoggerName(),
                event.getLevel().levelStr
        );
        LoggerDisruptorQueue.publishEvent(loggerMessage);
        return FilterReply.ACCEPT;
    }

}
